package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import admin.models.CategoryModel;

import entities.Author;
import entities.Category;
import entities.Question;

import models.AuthorModel;
import models.QuestionModel;

import systems.RecentlyQuestion;

public class RecentlyServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		List<Question> questions = RecentlyQuestion.getQuestions();
		
		List<String> aIds = new ArrayList<String>();
		List<String> cIds = new ArrayList<String>();
		
		for (int i = 0; i < questions.size(); i++) {
			aIds.add(questions.get(i).getAuthor());
			cIds.add(questions.get(i).getCategory());
		}
		
		//Get authors
		AuthorModel authorModel = AuthorModel.getInstance();
		Map<String, Author> authors = authorModel.getByListIds(aIds);
		
		//Get categories
		CategoryModel categoryModel = CategoryModel.getInstance();
		Map<String, Category> categories = categoryModel.getByListIds(cIds);
		
		request.setAttribute("questions", questions);
		request.setAttribute("authors", authors);
		request.setAttribute("categories", categories);
		
		try {
			request.getRequestDispatcher("/pages/recently.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
	}
}
